package psrc.logic.util;

import java.text.SimpleDateFormat;

public class Validador {

	public static boolean validateName(String name, Integer size, boolean notNull) {
		if (notNull && isEmpty(name))
			return false;
		if (name.length() > (size != null ? size : 100))
			return false;
		return true;
	}
	
	public static boolean validateReal(String valor, boolean notNull){
		if(notNull && isEmpty(valor))
			return false;
		try{
			
			Double.valueOf(valor.replaceAll(",", "."));
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public static boolean validateEstado(String estado, boolean notNull){
		if(notNull && isEmpty(estado))
			return false;
		if(estado.length() != 2 && !estado.matches("\\w")){
			return false;
		}
		return true;
	}

	public static boolean validateDate(String date, boolean notNull) {
		if (notNull && isEmpty(date))
			return false;
		try {
			SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
			dateformat.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public static boolean validateCNPJ(String cnpj, boolean notNull) {
		if (notNull && isEmpty(cnpj))
			return false;
		int soma = 0, dig;
		String cnpj_calc = cnpj.substring(0, 12);

		if (cnpj.length() != 14)
			return false;

		char[] chr_cnpj = cnpj.toCharArray();

		/* Primeira parte */
		for (int i = 0; i < 4; i++)
			if (chr_cnpj[i] - 48 >= 0 && chr_cnpj[i] - 48 <= 9)
				soma += (chr_cnpj[i] - 48) * (6 - (i + 1));
		for (int i = 0; i < 8; i++)
			if (chr_cnpj[i + 4] - 48 >= 0 && chr_cnpj[i + 4] - 48 <= 9)
				soma += (chr_cnpj[i + 4] - 48) * (10 - (i + 1));
		dig = 11 - (soma % 11);

		cnpj_calc += (dig == 10 || dig == 11) ? "0" : Integer.toString(dig);

		/* Segunda parte */
		soma = 0;
		for (int i = 0; i < 5; i++)
			if (chr_cnpj[i] - 48 >= 0 && chr_cnpj[i] - 48 <= 9)
				soma += (chr_cnpj[i] - 48) * (7 - (i + 1));
		for (int i = 0; i < 8; i++)
			if (chr_cnpj[i + 5] - 48 >= 0 && chr_cnpj[i + 5] - 48 <= 9)
				soma += (chr_cnpj[i + 5] - 48) * (10 - (i + 1));
		dig = 11 - (soma % 11);
		cnpj_calc += (dig == 10 || dig == 11) ? "0" : Integer.toString(dig);

		return cnpj.equals(cnpj_calc);
	}

	public static boolean validateNumber(String number, boolean notNull) {
		if (notNull && isEmpty(number))
			return false;
		return number.matches("[0-9]{1,150}");
	}

	public static boolean validateTelNumber(String number, boolean notNull) {
		if (notNull && isEmpty(number))
			return false;
		return number.matches("\\d{8,10}");
	}

	public static boolean validateEmail(String email, boolean notNull) {
		if (notNull && isEmpty(email))
			return false;
		return email.matches("^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$");
	}
	
	public static boolean validateCep(String cep, boolean notNull) {
		if(notNull && isEmpty(cep)){
			return false;
		}
		return cep.matches("[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]");
	}

	public static boolean isEmpty(String param) {
		if (param == null)
			return true;
		else if (param.equals(""))
			return true;
		return false;
	}

	

}
